package test;
/***********************************************************************/
/* Copyright (c) 1999-2005, The Rector and Board of Visitors of the    */
/* University of Virginia.                                             */
/* All rights reserved.                                                */
/*                                                                     */
/* This program is being made available under the terms of version 2.1 */
/* of theGNU Lesser General Public License ("LGPL") as published as of */
/* February, 1999 by the Free Software  Foundation; and subject to the */
/* restrictions of the LGPL, you are authorized to  modify and/or      */
/* redistribute it.                                                    */
/*                                                                     */
/* As stated in the LGPL, THIS PROGRAM IS BEING MADE AVAILABLE "AS IS",*/
/* AND WITHOUT A WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,    */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            */
/* MERCHANTABILITY AND  FITNESS FOR A PARTICULAR PURPOSE. FURTHER, YOU */
/* AGREE NOT TO HOLD THE COPYRIGHT  OWNER(S) LIABLE FOR DAMAGES,       */
/* INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,*/
/* ARISING OUT OF THE USE OR INABILITY TO USE  THE PROGRAM.            */
/* See the GNU General Public License for more details.                */
/*                                                                     */
/* You should have received a copy of version 2.1 of the GNU General   */
/* Public License along with this program; if not, you may obtain a    */
/* copy  from the Free Software Foundation website at the following URL*/
/* address (link  active as of September 1, 2005):                     */
/* http://www.fsf.org/licensing/licenses/lgpl.html; or that failing,   */
/* you can request a copy from Free Software Foundation,  Inc., 51     */
/* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                */
/*                                                                     */
/* This program is part of the HyperCast software distribution. To     */
/* learn more about HyperCast, please visit the website at             */
/* http://www.hypercast.org.                                           */
/***********************************************************************/

/** HelloWorldApp
 *
 *       The HyperCast HelloWorld programs demonstrate various aspects of
 *       the HyperCast API.  The programs create an overlay socket that
 *       sends and receives messages.
 *
 *       The HelloWorldApp program creates an overlay socket and joins an
 *       overlay network in the constructor. As a result, the program 
 *       is very short. 
 *       This file corresponds to an example program in the HyperCast API description
 *       (see Chapter:  "API - The Basics", Section: "The HyperCast Application").
 *
 * @author HyperCast Team
 * @version 2005 (version 3.0)
 *
 */

import hypercast.I_OverlayMessage;
import hypercast.HyperCastAppl;

public class HelloWorldApp extends HyperCastAppl {

	// Called by HyperCast (socket adapter thread).
	// Extended from HyperCastAppl to handle incoming messages.
	public void messageArrived(I_OverlayMessage message) {
		System.out.println(getLogicalAddress().toString() + ": Received \""
				+ new String(message.getPayload()) + "\" from "
				+ message.getSourceAddress().toString());

	}

	// The constructor creates an overlay socket that joins the overlay network 
	// The socket waits until it is stable and starts sending messages
	public HelloWorldApp(String cfile) {
		
		// This constructor creates an overlay socket that joins the overlay network 
		super(cfile); 
		
		// Block the program until the overlay socket has reached a "stable state" 
		// Note: The event NODE_ISSTABLE is not defined for all overlay protocols.

		waitUntil_NODE_ISSTABLE();
		System.out.println("HelloWorldApp: achieved stability.");

		// Transmit multicast messages forever 
		// Note: Different from some other programs, the message is created only once.
		byte[] messagePayload = "Hello World".getBytes();
		I_OverlayMessage message = createMessage(messagePayload);

		while (true) {
			sendToAll(message);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
		}
	}

	public static void main(String[] args) {

		//try {
		// Create a new HyperCast application  
		new HelloWorldApp("hypercast.xml");
		//}
		//catch (HyperCastException hce)
		//{
		//    System.err.println (hce);
		//}
	}
}